<?php

namespace App\Admin\Controllers;

use App\Service\Model\Cards;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Facades\Admin;
class CardsController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('绑卡信息列表')
            ->description('credit-card-lists')

            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content->header('信用卡')
            ->description('详情')
            ->body(Admin::show(Cards::findOrFail($id), function (Show $show) {
                $show->panel()
                ->tools(function ($tools) {
                    $tools->disableEdit();
                    $tools->disableDelete();
                });
        }));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑')
            ->description('信用卡')
            ->body($this->form()->edit($id));

    }

 
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Cards);
        $grid->id('ID')->sortable();
        $grid->tel('手机号');
        $grid->cardno('卡号');
        $grid->acctname('姓名');
        $grid->uid('用户ID');
        $grid->idno('身份证号');
        $grid->validdate('有效期');
        $grid->pay_date('还款日');
        $grid->bill_date('账单日');
        $grid->cvv2('CVV2');
        $grid->column('status','状态')->filter(Cards::status_text)->display(function(){ 
          return Cards::status_text[$this->status];
        })->setAttributes(['style' => 'color:red;']);
        $grid->column('created_at','绑定时间')->filter('range', 'date');

        $grid->disableRowSelector();
        $grid->disableCreateButton();
        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->disableEdit();
            $actions->getKey();
        });
        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 在这里添加字段过滤器
            $filter->like('cardno', '卡号');
            $filter->like('tel', '手机号');
        });

        $grid->quickSearch('tel','cardno','acctname');


        $grid->model()->orderBy('id','desc');
       
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $grid = new Show(Cards::findOrFail($id));
        $grid->id('ID');
        $grid->tel('手机号');
        $grid->cardno('卡号');
        $grid->acctname('姓名');
        $grid->idno('身份证号');
        $grid->validdate('有效期');
        $grid->pay_date('还款日');
        $grid->bill_date('账单期');
        $grid->ret('接口响应');
        $grid->created_at('绑定时间');
        return $grid;
    }

}
